package com.example.administrator.x2picturetransliteration.Home.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.x2picturetransliteration.Base.BaseActivity;
import com.example.administrator.x2picturetransliteration.Base.BasePresenter;
import com.example.administrator.x2picturetransliteration.Home.Dialog.OkDialog;
import com.example.administrator.x2picturetransliteration.Home.Presenter.Activity.SecurityManagementPresenter;
import com.example.administrator.x2picturetransliteration.Home.Presenter.Fragment.MePresenter;
import com.example.administrator.x2picturetransliteration.R;
import com.example.administrator.x2picturetransliteration.Util.RegularUtil;
import com.example.administrator.x2picturetransliteration.Util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 安全管理
 * Created by Administrator on 2019/8/27.
 */

public class SecurityManagementActivity extends BaseActivity{

    @BindView(R.id.TitleText)TextView TitleText;
    @BindView(R.id.LButton)ImageView LButton;

    @BindView(R.id.VerificationCodeText)TextView VerificationCodeText;

    @BindView(R.id.MobilePhoneEdit)EditText MobilePhoneEdit; //手机号
    @BindView(R.id.VerificationCodeEdit)EditText VerificationCodeEdit; //验证码
    @BindView(R.id.PwdEdit)EditText PwdEdit; //密码

    private MePresenter mMePresenter;

    private SecurityManagementPresenter mSecurityManagementPresenter;

    @Override
    public int intiLayout() {
        return R.layout.home_activity_security_management;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TitleText.setText("安全管理");
        LButton.setImageResource(R.mipmap.home_backw);

        mSecurityManagementPresenter = new SecurityManagementPresenter(this,mCompositeSubscriptions);

        mMePresenter = new MePresenter(this,mCompositeSubscriptions);
    }



    /** 获取验证码 */
    @OnClick(R.id.VerificationCodeText)
    public void VerificationCodeTextClick(){

        if (CodeJudge){

            if ("".equals(MobilePhoneEdit.getText() + "")){

                new ToastUtil(this).getToast(false,"请输入手机号").show();
                return;
            }

            Map<String,String> map = new HashMap<>();

            map.put("mobile",MobilePhoneEdit.getText() + "");

            mSecurityManagementPresenter.sendResetSmsCode(map, new BasePresenter.Callback() {
                @Override
                public void getData(Object data) {

                    StyleCodeText();
                }
            });
        }
    }

    /** 完成 */
    @OnClick(R.id.okText)
    public void okTextClick(){

        if ("".equals(MobilePhoneEdit.getText() + "")){

            new ToastUtil(this).getToast(false,"请输入手机号").show();
            return;

        }else if("".equals(VerificationCodeEdit.getText() + "")){

            new ToastUtil(this).getToast(false,"请输入验证码").show();
            return;

        }else if("".equals(PwdEdit.getText() + "")){

            new ToastUtil(this).getToast(false,"请输入密码").show();
            return;
        }

        if (RegularUtil.isENG_NUM(PwdEdit.getText() + "") && (PwdEdit.getText() + "").length()>=8 && (PwdEdit.getText() + "").length()<=12){

            Map<String,String> map = new HashMap<>();

            map.put("mobile",MobilePhoneEdit.getText() + "");
            map.put("code",VerificationCodeEdit.getText() + "");
            map.put("password",PwdEdit.getText() + "");

            mSecurityManagementPresenter.resetPassword(map);

        }else {

            new ToastUtil(this).getToast(false,"密码格式错误").show();
        }
    }

    /** 返回 */
    @OnClick(R.id.left_button)
    public void left_buttonClick(){

        finish();
    }

    /** 注销 */
    @OnClick(R.id.zxText)
    public void zxTextClick(){

        new OkDialog(this, "注销后不可恢复，是否确认注销?", new OkDialog.hd() {
            @Override
            public void hdff() {

                mMePresenter.remove();
            }
        }).show();
    }

    /** 点击获取验证码时候获取更新文本状态 */
    private volatile boolean CodeJudge = true;
    private void StyleCodeText(){

        CodeJudge = false;

        final Handler handler = new Handler();

        new Thread(new Runnable() {
            @Override
            public void run() {

                for (int i=60;i>0;i--){

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    final int finalI = i;

                    try{

                        if(VerificationCodeText==null)break;

                        handler.post(new Runnable() {
                            @Override
                            public void run() {

                                if(VerificationCodeText==null)return;

                                if (finalI ==1){

                                    VerificationCodeText.setTextColor(getResources().getColor(R.color.SecurityManagementTextColor3));
                                    VerificationCodeText.setText("获取验证码");
                                    CodeJudge = true;
                                }else {

                                    VerificationCodeText.setTextColor(getResources().getColor(R.color.SecurityManagementTextColor2));
                                    VerificationCodeText.setText(finalI + "s");
                                }
                            }
                        });

                    }catch (Exception e){

                    }
                }

            }
        }).start();
    }
}
